package com.uzai.console.service.wxpubmanage.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.wxpubmanage.WxpubComponentDto;
import com.uzai.console.entity.wxpubmanage.WxpubComponentEntity;
import com.uzai.console.mapper.WxpubComponentMapper;
import com.uzai.console.service.wxpubmanage.WxpubComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author cen
 * @Date 2022/3/22 0022 10:19
 */
@Service
public class WxpubComponentServiceImpl implements WxpubComponentService {

    @Autowired
    private WxpubComponentMapper mapper;

    @Override
    public WxpubComponentEntity queryObject(Long merId) {
        WxpubComponentEntity entity = mapper.queryObject(merId);
        return entity == null ? new WxpubComponentEntity() : entity;
    }

    @Override
    public void updateObject(WxpubComponentDto dto) {

        WxpubComponentEntity wxpubComponentEntity = mapper.queryObject(dto.getMerId());
        WxpubComponentEntity entity = JSONObject.parseObject(JSONObject.toJSONString(dto), WxpubComponentEntity.class);
        int nowTime = DateUtil.getNowTime();
        entity.setUpdateTime(nowTime);

        if(wxpubComponentEntity == null ) {
            entity.setId(IdWorker.getId());
            entity.setCreateTime(nowTime);
            mapper.addObject(entity);
        }else {
            mapper.updateObject(entity);
        }
    }

}
